<template>
  <div class="report">
    <el-form
      ref="reportForm"
      label-width="80px"
      :model="reportForm"
      :rules="rules"
      hide-required-asterisk
    >
      <el-form-item prop="reason" label="原因">
        <el-radio-group v-model="reportForm.reason" class="radio-group">
          <el-radio
            v-for="item in reasonList"
            :key="item.value"
            :label="item.value"
            name="reason"
          >{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="描述(选填)">
        <el-input v-model="reportForm.description" type="textarea" rows="5" show-word-limit maxlength="255" />
      </el-form-item>
      <el-button type="primary" style="width: 100%;" :loading="isButtonLoading" @click="handleSubmit">提交</el-button>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { saveReport } from '@/api/report'
import { getDictList } from '@/api/dict'
import { formatDateTime } from '@/utils/dateUtil'
export default {
  name: 'Report',
  data() {
    return {
      reasonList: [],
      reportForm: {
        postId: this.$route.query.postId,
        reason: undefined,
        description: undefined,
        createDate: undefined
      },
      isButtonLoading: false,
      rules: {
        reason: [{
          message: '请选择原因',
          required: true,
          trigger: 'blur'
        }]
      }
    }
  },
  computed: {
    ...mapGetters(['token'])
  },
  beforeMount() {
    this.getReasonList()
  },
  methods: {
    handleSubmit() {
      this.$refs.reportForm.validate(valid => {
        if (valid) {
          this.reportForm.createDate = formatDateTime(new Date())
          this.isButtonLoading = true
          saveReport(this.reportForm).then(res => {
            history.back()
          }, () => {}).finally(() => {
            this.isButtonLoading = false
          })
        } else {
          console.log('表单验证有误')
        }
      })
    },
    async getReasonList() {
      const { data } = await getDictList({
        searches: [
          {
            key: 'type',
            rule: '=',
            value: 'report_reason'
          }
        ]
      })
      this.reasonList = data
    }
  }
}
</script>

<style lang="scss" scoped>
.report {
    background-color: #fff;
    width: 50%;
    margin: 20px auto;
    padding: 20px;
    border-radius: 20px;
    box-shadow: 0 0 10px black;
    .radio-group {
      display: flex;
      flex-wrap: wrap;
      .el-radio {
        width: 20%;
        margin: 10px;
      }
    }
}
</style>
